/*
 * Copyright (c) 2021 KaiHong Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "savewifi.h"
#ifndef CONFIG_FACTORY_TEST_MODE
#include "lwip/opt.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#endif
#include <hi_flash.h>
#include <hi_nv.h>
#include <hi_lowpower.h>
#include <hi_diag.h>
#include <hi_crash.h>
#include <hi_sal.h>
#include <hi_shell.h>
#include <hi_fs.h>
#include "hi_wifi_api.h"
#include <hi_partition_table.h>
#include <hi_ver.h>
#include <hi_cpu.h>
#include <hi_crash.h>
#include <hi_early_debug.h>
#include <hi_tsensor.h>

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "cmsis_os2.h"

#include "wifi_device.h"
#include "wifi_hotspot.h"
#include "wifi_error_code.h"
#include "lwip/netifapi.h"
#include "softap.h"
#include "sta.h"
#include "savewifi.h"


#define WIFI_SAVE_SSID "MY_SSID"
#define WIFI_SAVE_PWSD "MY_PWSD"
char SSID_buff[20]={0};
char SSID_pwsd[20]={0};
char SAVE_BUFF[20]={0};

int GET_WIFI_SSID()
{
    hi_s32 fd = hi_open(WIFI_SAVE_SSID,HI_FS_O_RDWR);

    if(fd < 0)
    {
      printf("NO WIFI SAVE!\r\n");
      printf("errno 0x%x\n", hi_get_fs_error());
      return -1;
    }
    if (hi_read(fd, (hi_char *)SSID_buff, sizeof(SSID_buff)) < 0) 
    {
        printf("errno 0x%x\n", hi_get_fs_error());
        return -1;
    }

    if (hi_close(fd) < 0) 
    {
        printf("errno 0x%x\n", hi_get_fs_error());
    return -1;
    }
    printf("THE SSID is %s",SSID_buff);
    return 1;

}

int GET_WIFI_PWSD()
{
    hi_s32 fd = hi_open(WIFI_SAVE_PWSD,HI_FS_O_RDWR);

    if(fd < 0)
    {
      printf("NO WIFI SAVE!\r\n");
      return -1;
    }
    if (hi_read(fd, (hi_char *)SSID_pwsd, sizeof(SSID_pwsd)) < 0) 
    {
        printf("errno 0x%x\n", hi_get_fs_error());
        return -1;
    }

    if (hi_close(fd) < 0) 
    {
        printf("errno 0x%x\n", hi_get_fs_error());
    return -1;
    }
    printf("THE PWSD is %s",SSID_pwsd);
    return 1;

}

void WR_WIFI_SSID(char *the_ssid)
{
    hi_s32 fd = hi_open(WIFI_SAVE_SSID,HI_FS_O_RDWR|HI_FS_O_TRUNC| HI_FS_O_CREAT);

    if(fd < 0)
    {
      printf("NO WIFI SAVE!\r\n");
      return;
    }
    if (hi_write(fd, the_ssid,20) < 0) 
    {
    printf("errno 0x%x\n", hi_get_fs_error());
    return;
    }
     if (hi_close(fd) < 0) 
    {
        printf("errno 0x%x\n", hi_get_fs_error());
    return;
    }
}

void WR_WIFI_PWSD(char *the_pwsd)
{
    hi_s32 fd = hi_open(WIFI_SAVE_PWSD,HI_FS_O_RDWR| HI_FS_O_TRUNC| HI_FS_O_CREAT);

    if(fd < 0)
    {
      printf("NO WIFI SAVE!\r\n");
      return;
    }
    if (hi_write(fd, the_pwsd,20) < 0) 
    {
    printf("errno 0x%x\n", hi_get_fs_error());
    return;
    }
     if (hi_close(fd) < 0) 
    {
        printf("errno 0x%x\n", hi_get_fs_error());
    return;
    }
}

int LINK_WIFI()
{
    if(GET_WIFI_SSID()<0)
    {
        printf("SSID Error\r\n");
        return -1;
    }
     if(GET_WIFI_PWSD()<0)
    {
        printf("PWSD Error\r\n");
        return -1;
    }
    printf("Connect to %s Sucessfully\r\n",SSID_buff);
    return 1;


    //WifiConnect(SSID_buff,SSID_pwsd);
    

}
static BOOL wifiaudio(void)
{
    //延时2S便于查看日志
    osDelay(200);
    WifiConnect(SSID_buff,SSID_pwsd);
}

void Wifi_audio_Demo(void)
{
    osThreadAttr_t attr;

    attr.name = "wifiaudio";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 20240;
    attr.priority = 25;

    if (osThreadNew((osThreadFunc_t)wifiaudio, NULL, &attr) == NULL)
    {
        printf("Falied to create WifiAPTask!\r\n");
    }
}


